package com.auxgroup.hr.front.site.co.process;

import com.auxgroup.hr.front.commons.BaseRetCode;
import com.auxgroup.hr.front.commons.vo.BaseResponseVO;
import com.auxgroup.hr.front.dao.domain.PsCFqyySub;
import com.auxgroup.hr.front.dao.repository.PsCFqyySubRepository;
import com.auxgroup.hr.front.dozer.IObjectMapper;
import com.auxgroup.hr.front.site.co.BaseWfCO;
import com.auxgroup.hr.front.site.vo.process.PsCFqyySubVO;
import oracle.jdbc.proxy.annotation.Post;
import org.sonatype.aether.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("${auxgroup.hr.contextPath}/fqyy")
public class FqyyCO extends BaseWfCO {

    @Autowired
    private PsCFqyySubRepository psCFqyySubRepository;

    @Resource(name = "dozerMapper")
    protected IObjectMapper objectMapper;

    /**
     * 提交分权引用信息
     */
    @PostMapping("")
    public BaseResponseVO saveFqyyInfo(@RequestBody PsCFqyySub psCFqyySub) {

        String cTransactionNbr = psCFqyySub.getcTransactionNbr();
        if (StringUtils.isEmpty(cTransactionNbr)) {
            return buildRespone(BaseRetCode.CODE_ERROR_GET_TRANSACTION_NBR_FAILED);
        }


        PsCFqyySub psCFqyySubNew = psCFqyySubRepository.findOne(cTransactionNbr);
        if (psCFqyySubNew == null) {
            psCFqyySubNew = new PsCFqyySub();
        }
        psCFqyySubNew.setcFqyyFlag("有".equals(psCFqyySub.getcFqyyFlag())?"Y":"N");
        psCFqyySubNew.setcFqyyDetail(psCFqyySub.getcFqyyDetail());
        psCFqyySubNew.setcTransactionNbr(psCFqyySub.getcTransactionNbr());

        psCFqyySubRepository.save(psCFqyySubNew);

        return buildRespone(psCFqyySubNew);
    }


    /**
     * 获取分权引用信息
     */
    @GetMapping("")
    public BaseResponseVO getFqyyInfo(String cTransactionNbr,String processType) {

        PsCFqyySub psCFqyySub = new PsCFqyySub();
        //获取分权信息
        if (!StringUtils.isEmpty(cTransactionNbr)) {
            psCFqyySub = psCFqyySubRepository.findOne(cTransactionNbr);
            if (psCFqyySub != null) {
                psCFqyySub.setcFqyyFlag(psCFqyySub.getcFqyyFlag().equals("Y") ? "有" : "无");
                psCFqyySub.setcFqyyDetail(psCFqyySub.getcFqyyDetail());
            } else {
                psCFqyySub = new PsCFqyySub();
                psCFqyySub.setcFqyyFlag("无");
            }
        }

        PsCFqyySubVO psCFqyySubVO = objectMapper.map(psCFqyySub,PsCFqyySubVO.class);

        Integer processToatalSum = sysDictService.getProcessTotalSum(processType);
        Integer nofqyySum = sysDictService.getNoFqyySum(processType);
        if (processToatalSum == 0) {
            psCFqyySubVO.setNoFqyySum("0");
            psCFqyySubVO.setNoFqyyRatio("0");
        } else {
            psCFqyySubVO.setNoFqyySum(nofqyySum.toString());
            psCFqyySubVO.setNoFqyyRatio(Math.round(nofqyySum * 10000.0 / processToatalSum) * 1.0 / 100 + "");
        }

        return buildRespone(psCFqyySubVO);
    }

    @Override
    protected Set<String> getAuthFileds() {
        return null;
    }
}
